<?php


namespace app\api\services;

use app\admin\traits\HtnsTrait;
use think\Exception;
use think\facade\Cache;

/**
 * Class HtnsService
 * @package app\api\services
 * @desc 海淘系统相关服务
 */
class HtnsService
{
    use HtnsTrait;

    /**
     * @desc 登录海淘女神系统后台
     * @return array
     */
    public function login()
    {
        try {
            // 查询原系统订单详情接口
            $domain         = sysconfig('other', 'domain');// 域名
            $token          = '';
            $cacheKeyToken  = 'htnsToken0802';
            $cacheDataToken = Cache::get($cacheKeyToken);
            if (empty($cacheDataToken)) {
                // 先登录
                $url                         = $domain . '/adminapi/login';
                $data['account']             = 'admin';
                $data['pwd']                 = '123456';
                $data['imgcode']             = '0000';
                $data['key']                 = '64c7d1a21b21f';
                $data['captchaType']         = 'blockPuzzle';
                $data['captchaVerification'] = '';
                $loginData                   = $this->httpCurlPost($url, $data);
                $loginDataArr                = json_decode($loginData, true);
                if ($loginDataArr['status'] == 200) {
                    $token = $loginDataArr['data']['token'];
                    // 存储token
                    Cache::set($cacheKeyToken, $token, 3600 * 24);
                }
            } else {
                $token = $cacheDataToken;
            }
            $header[] = 'Authori-zation:Bearer ' . $token;
            return $this->returnArr(1, 'HtnsService/login登录成功', $header);

        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/login服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 查询订单物流信息
     * @param int $id 订单id
     * @param int $orderStatus 订单状态
     * @param array $header 授权头文件
     * @return array
     */
    public function getExpress($id, $orderStatus = 0, $header = [])
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            $kdurls = $domain . '/adminapi/order/express/' . $id;

            if (empty($header)) {
                $resArr = $this->login();
                if ($resArr['code']) {
                    $header = $resArr['data'];
                }
            }
            $expressData    = $this->httpCurlGet($kdurls, 1, $header);
            $expressDataArr = json_decode($expressData, true);

            if ($expressDataArr['status'] == 200) {
                if ($expressDataArr['data']['result']) {
                    $rest['expressStatu'] = '已发货';
                }
                foreach ($expressDataArr['data']['result'] as $v) {
                    if (strstr($v['status'], '签收')) {
                        $rest['expressStatu'] = '已签收';
                    }
                }
                $rest['express'] = $expressDataArr['data']['result'];

                // 快递已签收,订单状态未签收
                if ($rest['expressStatu'] == '已签收' && $orderStatus != 2) {
                    // 调用接口收货
                    $r = $this->takeOrder($id, $header);
                    if ($r['code']) {
                        $rest['shStatu'] = 'PUT方法收货成功';
                    }
                }
            }
            return $this->returnArr(1, 'HtnsService/getExpress查询物流成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/getExpress服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 订单签收
     * @param int $id 订单id
     * @param array $header 授权头文件
     * @return array
     */
    public function takeOrder($id, $header = [])
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/adminapi/order/take/' . $id;
            if (empty($header)) {
                $resArr = $this->login();
                if ($resArr['code']) {
                    $header = $resArr['data'];
                }
            }
            $shData    = $this->httpCurlGet($shurls, 1, $header, 'PUT');
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'PUT方法收货成功';
            }
            return $this->returnArr(1, 'HtnsService/takeOrder订单签收成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/takeOrder订单签收服务异常,' . $e->getMessage());
        }
    }
    /**
     * @desc 全国快递查询
     * @param  string $no 快递单号
     * @param string $type 快递公司简称
     */
    public function getNationalExpress($no , $type = '')
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/api/kdi?no=' . $no.'&type='.$type;
            $shData    = $this->httpCurlGet($shurls, 1);
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['issign'] = $shDataArr['data']['issign'];
                $rest['express'] = $shDataArr['data']['list'];
            }
            return $this->returnArr(1, 'HtnsService/getNationalExpress全国快递查询成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/getNationalExpress全国快递查询服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 仅退款-立即退款
     * @param int $id 退款表id
     * @param string $order_id 订单号
     * @param string $refund_price 退款金额
     * @return array
     */
    public function refundAgree($id , $order_id, $refund_price, $header = [])
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/adminapi/jizhan/refund/' . $id;
            if (empty($header)) {
                $resArr = $this->login();
                if ($resArr['code']) {
                    $header = $resArr['data'];
                }
            }
            $dataArr['order_id'] = $order_id;
            $dataArr['refund_price'] = $refund_price;
//            $data = json_encode($dataArr);
            $shData    = $this->httpCurlPost($shurls, $dataArr, $header);
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'POST方法-仅退款-立即退款成功';
            }else{
                return $this->returnArr(0, 'HtnsService/refundAgree仅退款-立即退款失败,' .$shDataArr['msg']);
            }
            return $this->returnArr(1, 'HtnsService/refundAgree仅退款-立即退款成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/refundAgree仅退款-立即退款服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 仅退款-不同意退款
     * @param int $id 退款表id
     * @param string $order_id 订单号
     * @param string $refuse_reason 拒绝原因
     * @return array
     */
    public function refundNoAgree($id , $order_id, $refuse_reason , $header = [])
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
//            $shurls = $domain . '/adminapi/refund/no_refund/' . $id;
            $shurls = $domain . '/adminapi/jizhan/no_refund/' . $id;
            if (empty($header)) {
                $resArr = $this->login();
                if ($resArr['code']) {
                    $header = $resArr['data'];
                }
            }
            $dataArr['order_id'] = $order_id;
            $dataArr['refund_reason'] = $refuse_reason;
            $shData    = $this->httpCurlPost($shurls, $dataArr, $header);
            $shDataArr = json_decode($shData, true);
//            halt('refundNoAgree',$header,$shurls,$dataArr,$shData);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'POST方法-仅退款-不同意退款成功';
            }else{
                return $this->returnArr(0, 'HtnsService/refundNoAgree仅退款-不同意退款失败,' .$shDataArr['msg']);
            }
            return $this->returnArr(1, 'HtnsService/refundNoAgree仅退款-不同意退款成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/refundNoAgree仅退款-不同意退款服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 同意-退货退款
     * @param int $id 退款表id
     * @return array
     */
    public function refundAgreeProduct($id , $header = [])
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/adminapi/refund/agree/' . $id;
            if (empty($header)) {
                $resArr = $this->login();
                if ($resArr['code']) {
                    $header = $resArr['data'];
                }
            }
            $shData    = $this->httpCurlGet($shurls , 1, $header);
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'GET方法-仅退款-立即退款成功';
            }else{
                return $this->returnArr(0, 'HtnsService/refundAgreeProduct同意-退货退款失败,' .$shDataArr['msg']);
            }
            return $this->returnArr(1, 'HtnsService/refundAgreeProduct同意-退货退款成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/refundAgreeProduct仅退款-立即退款服务异常,' . $e->getMessage());
        }
    }
}